package com.css.fxfzypg.manage.disasterrisk.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzypg.base.attachment.service.AttachmentInfoService;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.manage.disasterrisk.repository.entity.DisasterRiskEntity;
import com.css.fxfzypg.manage.disasterrisk.service.DisasterRiskService;
import com.css.fxfzypg.manage.disasterrisk.vo.DisasterRiskParams;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/disasterRisk")
@Slf4j
public class DisasterRiskController {
    @Autowired
    DisasterRiskService disasterRiskService;
    @Autowired
    AttachmentInfoService attachmentInfoService;
    @Autowired
    HttpServletRequest request;

    /**
     * 判断该调研点在哪个任务区域中,根据经纬度去判断
     * @param x
     * @param y
     * @return
     */
  /* @GetMapping("/checkPoint")
    public RestResponse checkPoint(String x,String y) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{

            Map<String, Object> map = disasterRiskService.checkPoint(x, y);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("风险点和区域校验失败!",e);
            json.put("message", "风险点和区域校验失败!");
            response=RestResponse.succeed(json);
        }

        return response;
    }*/

    /**
     * 新增数据
     * @param disasterRiskEntity
     * @return
     */
    @PostMapping("/insertDisasterRisk")
    public RestResponse insertDisasterRisk(@RequestBody DisasterRiskEntity disasterRiskEntity) {
        RestResponse response = null;
        try{
            boolean res = disasterRiskService.saveDisasterRisk(disasterRiskEntity);
            if(res){
                response = RestResponse.succeed("保存成功!");
                response.setMessage("保存成功!");
            }else {
                response = RestResponse.fail("保存失败!");
            }
        }catch (Exception e){
            String errormessage = "保存失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 保存并提交
     * @param disasterRiskEntity
     * @return
     */
    @PostMapping("/saveAndSubmit")
    public RestResponse saveAndSubmit(@RequestBody DisasterRiskEntity disasterRiskEntity) {
        RestResponse response = null;
        try{
            boolean res = disasterRiskService.saveAndSubmit(disasterRiskEntity);
            if(res){
                response = RestResponse.succeed("保存并提交成功!");
                response.setMessage("保存并提交成功!");
            }else {
                response = RestResponse.fail("保存并提交失败!");
            }
        }catch (Exception e){
            String errormessage = "保存并提交失败!";
            log.error(errormessage,e);
            response=RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 查询已提交和未提交列表
     * @param request
     * @param submitFlag
     * @param userId
     * @return
     */
    @GetMapping("/querySubmitOrNot")
    public RestResponse querySubmitOrNot(HttpServletRequest request,String submitFlag,String userId) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = disasterRiskService.querySubmitOrNot(submitFlag,curPage,pageSize,userId);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("获取列表信息失败!",e);
            response=RestResponse.fail("获取列表信息失败!");
        }
        return response;
    }

    /**
     * 根据id更新提交状态
     * @param map
     * @return
     */
    @PostMapping("/updateIsSubmit")
    public RestResponse updateIsSubmit(@RequestBody Map<String,String> map) {
        RestResponse response = null;
        try {
            boolean res = disasterRiskService.updateIsSubmit(map);
            if(res){
                response = RestResponse.succeed("提交成功!");
                response.setMessage("提交成功!");
            }else {
                response = RestResponse.fail("提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "提交失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 根据id查询详情页数据
     * @param id
     * @return
     */
    @GetMapping("/queryData")
    public RestResponse queryData(String id) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            //获取业务表数据
            DisasterRiskEntity disasterRiskEntity = disasterRiskService.queryData(id);
            //获取附件
            if(PlatformObjectUtils.isEmpty(disasterRiskEntity.getFile())){
                json.put("file","");
            }else{
                List<AttachmentInfoEntity> attachmentInfoEntityList = attachmentInfoService.queryFile(disasterRiskEntity.getFile());
                json.put("file",attachmentInfoEntityList);
            }
            json.put("data",disasterRiskEntity);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
    /**
     * 修改详情页数据
     * @param disasterRiskEntity
     * @return
     */
    @PostMapping("/modifyData")
    public RestResponse modifyData(@RequestBody DisasterRiskEntity disasterRiskEntity) {
        RestResponse response = null;
        try {
            boolean res = disasterRiskService.modifyData(disasterRiskEntity);
            if(res){
                response = RestResponse.succeed("修改成功!");
                response.setMessage("修改成功!");
            }else {
                response = RestResponse.fail("修改失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
    /**
     * 修改并提交
     * @param disasterRiskEntity
     * @return
     */
    @PostMapping("/modifyAndSubmit")
    public RestResponse modifyAndSubmit(@RequestBody DisasterRiskEntity disasterRiskEntity) {
        RestResponse response = null;
        try {
            boolean res = disasterRiskService.modifyAndSubmit(disasterRiskEntity);
            if(res){
                response = RestResponse.succeed("修改并提交成功!");
                response.setMessage("修改并提交成功!");
            }else {
                response = RestResponse.fail("修改并提交失败!");
            }
        } catch (Exception e) {
            String errormessage = "修改并提交失败!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
    /**
     * 根据id删除数据
     * @param
     * @return
     */
    @GetMapping("/deleteDisasterRisk")
    public RestResponse deleteDisasterRisk(String ids) {
        RestResponse response = null;
        try{
            boolean res = disasterRiskService.deleteDisasterRisk(ids);
            if(res){
                response = RestResponse.succeed("删除成功!");
                response.setMessage("删除成功!");
            }else {
                response = RestResponse.fail("删除失败!");
            }
        }catch (Exception e){
            log.error("删除失败!",e);
            response=RestResponse.fail("删除失败!");
        }
        return response;
    }

    /**
     * 灾害风险点数据列表查询
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/queryDisasterRiskList")
    public RestResponse queryDisasterData(HttpServletRequest request, DisasterRiskParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = disasterRiskService.queryDisasterData(queryParams,curPage,pageSize);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            log.error("查询灾害风险点数据失败!",e);
            response=RestResponse.fail("查询灾害风险点数据失败!");
        }
        return response;
    }
}